import BaseView from './baseview.js'
import MainView from './main.js';

export default class Login extends BaseView{
  
      constructor(id){
            super(id);
            super.init();
      }

     render(){
        this.$app.html(`
        <div style="margin:150px auto;width: 400px;">
        <form class="layui-form">
            <div class="layui-form-item">
              <label class="layui-form-label">用户名</label>
              <div class="layui-input-inline">
                <input type="text" id="username" name="title" required  lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">密&nbsp;码</label>
              <div class="layui-input-inline">
                <input type="password" id="pwd" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
              </div>
    
            </div>
            <div class="layui-form-item">
              <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo" id="btn" type="button">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
              </div>
            </div>
          </form>
        </div>
        `)
     }

     //绑定事件
     handle(){
        $("#btn").click(()=>{
            $.ajax({
                  type:"post",
                  url:"/api/user/login",
                  data:{
                      "userName":$("#username").val(),
                      "pwd":$("#pwd").val()
                  },
                  dataType:"json",
                  success(result){
                      console.log(result);
                   //判断是否登录成功
                    if(result.code===0){
                        alert(result.msg);
                        //new MainView("myapp");
                        window.location.hash="#/main";
                   }else{
                    alert(result.msg);                   }  
                  }
              })
          });
      }
     }
   
     //new Login("myapp");
